﻿namespace Web.Control.WWW.Answers
{
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using Lx.Web.Common.ControlsUI;
    using System.Collections;
    using BLF.WWW_WWW.Answer;
    using Model.WWW_WWW.Answers;
    using System.Collections.Generic;

    /// <summary>
    /// Question 的摘要说明
    /// </summary>
    public class Question : BaseControl
    {
        /// <summary>
        /// 当前会员号
        /// </summary>
        protected string Memberno = "";
        protected int PageIndex = 1;
        /// <summary>
        /// 是否为提问自动转向过来
        /// </summary>
        protected bool MyFirstO = false;
        /// <summary>
        /// 问题详细信息(问题ID为下一个问题ID)
        /// </summary>
        protected QuestionModel QDetatil;
        /// <summary>
        /// 最佳答案
        /// </summary>
        protected QAnswerModel BestAnswer;
        /// <summary>
        /// 相关问题列表
        /// </summary>
        protected List<QuestionModel> QRelated=new List<QuestionModel>();
        /// <summary>
        /// 标签列表
        /// </summary>
        protected List<string> TagList = new List<string>();
        /// <summary>
        /// Cookie保存的学习语言
        /// </summary>
        protected string CokLearn = "";
        /// <summary>
        /// Cookie保存的使用语言
        /// </summary>
        protected string CokSpeak = "";
        /// <summary>
        /// 问题数量（不包括最佳答案）
        /// </summary>
        protected int AnswerCount = 0;
        /// <summary>
        /// 当前问题ID
        /// </summary>
        protected int qid = 0;
        /// <summary>
        /// 当前排序类型(1,Time:oldest to newest 2,Time: newest to oldest,3,Rating: hightest to lowest)
        /// </summary>
        protected int OrdType = 1;
        /// <summary>
        /// 已决问题数量
        /// </summary>
        protected int ResolveQCount = 0;
        /// <summary>
        /// 流行问题数量
        /// </summary>
        protected int PopularQCount = 0;
        protected Repeater rptAnswer;
        protected Repeater rptExpired;
        protected Repeater rptRelated;
        protected Repeater rptPopular;
        protected override void OnLoad(EventArgs e)
        {
            qid = 0;
            int aid = 0;
            int.TryParse(Request["QuesID"], out qid);
            int.TryParse(Request["BAId"], out aid);
            if (qid == 0)
            {
                Response.End();
                return;
            }
            if (aid != 0)
            {
                if (HttpContext.Current.User.Identity.Name == "")
                {
                    Response.Redirect(string.Format("/user/login.htm?ReturnUrl=/answers/question/{0}-1-1-2.htm?BestAnswerId={1}", qid, aid));
                    return;
                }
            }
            
            if (!string.IsNullOrEmpty(Request["page"]))
            {
                if (Request["page"] == "L")
                {
                    PageIndex = 999;
                }
                else
                {
                    int.TryParse(Request["page"], out PageIndex);
                    if (PageIndex == 0)
                    {
                        PageIndex = 1;
                    }
                }
            }
            
            string orderSql = "PostDate";
            if(!string.IsNullOrEmpty(Request["OrderType"]))
            {
                switch (Request["OrderType"].ToString())
                {
                    case "1":
                        OrdType = 1;
                        orderSql = "PostDate";
                        break;
                    case "2":
                        OrdType = 2;
                        orderSql = "PostDate desc";
                        break;
                    case "3":
                        OrdType = 3;
                        orderSql = "Point desc";
                        break;
                }
            }
            bool norecord = false;
            if (!string.IsNullOrEmpty(Request["noRecord"]))
            {
                if (Request["noRecord"] == "2")
                {
                    norecord = true;
                }
                if (Request["noRecord"] == "1")
                {
                    MyFirstO = true;
                }
            }
            Memberno = HttpContext.Current.User.Identity.Name;
            if (Request.Cookies["italkiLearn"] != null)
            {
                CokLearn = Request.Cookies["italkiLearn"].Value;
            }
            if (Request.Cookies["italkiSpeak"] != null)
            {
                CokSpeak = Request.Cookies["italkiSpeak"].Value;
            }
            string sessionID = "";
            string iP = "";
            string userAgent = "";
            string referer = "";
            if (!norecord)
            {
                sessionID = Session.SessionID;
                iP = Request.UserHostAddress;
                userAgent = Request.UserAgent;
                referer = HttpContext.Current.Request.UrlReferrer == null ? "" : HttpContext.Current.Request.UrlReferrer.AbsoluteUri;
            }
            ArrayList arrayList = Answers.GetQuestionDetail(Memberno, qid, PageIndex, 15, CokLearn, sessionID, iP, userAgent, referer, orderSql,aid);
            if (arrayList != null && arrayList.Count > 0)
            {
                QDetatil = arrayList[0] == null ? new QuestionModel() : arrayList[0] as QuestionModel;
                if (QDetatil.MemberNo == "")
                {
                    Response.End();
                    return;
                }
                TagList = arrayList[1] == null ? new List<string>() : arrayList[1] as List<string>;
                BestAnswer = arrayList[2] == null ? new QAnswerModel() : arrayList[2] as QAnswerModel;
                AnswerCount = arrayList[4] == null ? 0 : (int)arrayList[4];
                int pCount = (int)Math.Ceiling(((decimal)AnswerCount) / ((decimal)15));
                if (PageIndex > pCount) PageIndex = pCount;
                QRelated = arrayList[5] == null ? new List<QuestionModel>() : arrayList[5] as List<QuestionModel>;
                ResolveQCount = (arrayList[7] as List<QuestionModel>).Count;
                PopularQCount = (arrayList[6] as List<QuestionModel>).Count;
                //IE title
                string tags = "";
                for (int i = TagList.Count - 1; i >= 0; i--)
                {
                    tags += TagList[i] + ", ";
                }
                this.DynamicTitle = GetText("AS524", new string[] { QDetatil.Question, GetLanguageText(QDetatil.LanguageLearn), GetLanguageText(QDetatil.BaseLanguage) });
                this.DynamicKeywords = GetText("AS525", new string[] { tags, GetLanguageText(QDetatil.LanguageLearn), GetLanguageText(QDetatil.BaseLanguage) });
                this.DynamicDescription = GetText("AS526", new string[] { QDetatil.Question });

                if (rptAnswer != null)
                {
                    rptAnswer.DataSource = arrayList[3];
                    rptAnswer.DataBind();
                }
                if (rptExpired != null)
                {
                    rptExpired.DataSource = arrayList[7];
                    rptExpired.DataBind();
                }
                if (rptRelated != null)
                {
                    rptRelated.DataSource = QRelated;
                    rptRelated.DataBind();
                }
                if (rptPopular != null)
                {
                    rptPopular.DataSource = arrayList[6];
                    rptPopular.DataBind();
                }
            }
            else
            {
                Response.End();
                return;
            }
        }
    }
}
